//
//  YBLoginVC.swift
//  Moonlight
//
//  Created by Penlon Kim on 2023/6/5.
//  Copyright © 2023 Moonlight Game Streaming Project. All rights reserved.
//

import UIKit
import SnapKit
import SwiftyJSON
import SVProgressHUD
import EachNavigationBar

class YBLoginVC: BaseVC {
    
    // MARK: Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigation.bar.alpha = 0
        view.backgroundColor = .lightWhiteDark27
        view.addSubview(loginView)
        
        loginView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        #if DEBUG
        vm.loginModel.userName = "18912341234"
        vm.loginModel.password = "123456Aa"
        #endif
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.bk_setBackBarButtonItem(isHidden: true)
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        loginView.endEditing(true)
    }
    
    // MARK: - lazy
    lazy var vm: YBLoginViewModel = {
        let vm = YBLoginViewModel()
        return vm
    }()
    
    private lazy var loginView: YBLoginView = {
        let view = YBLoginView()
        view.delegate = self
        return view
    }()
    
}

// MARK: - YBLoginViewDelegate代理
extension YBLoginVC: YBLoginViewDelegate {
    
    func loginViewDidClick(_ btnType: YBLoginView.BtnType) {
        switch btnType {
        case .login:
            loginView.endEditing(true)
            self.post_login()
        case .register:
            let vc = YBRegisterVC()
            self.navigationController?.pushViewController(vc)
        case .oneClickLogin:
            let vc = YBOneClickLoginVC()
            self.navigationController?.pushViewController(vc)
        }
    }
    
    func loginViewInput(_ inputType: YBLoginRegisterInputView.InputType, content: String) {
        PPP("\(inputType.style.des): \(content)")
        switch inputType {
        case .phone:
            vm.loginModel.userName = content.removeAllSpace
        case .pwd:
            vm.loginModel.password = content.removeAllSpace
        case .code:
            break
        }
    }
    
}

// MARK: - 网络请求
extension YBLoginVC {
    
    fileprivate func post_login() {
        guard loginView.checkBtn.isSelected else {
            HintTool.toast("请勾选协议")
            return
        }
        guard let params = vm.validLoginParams({ msg in
            HintTool.toast(msg)
        }) else { return }
        
        SVProgressHUD.show(withStatus: "登录中...")
        vm.req_login(params) { ok, obj, msg, code in
            SVProgressHUD.dismiss()
            if ok {
                guard let js = obj as? JSON,
                      let data = js[NetDataKey].string else { return }
                HintTool.toast("登录成功")
                AppHelper.token = data
                
                TaskTool.delay(0.5) {
                    guard let window = UIApplication.shared.mainWindow() else { return }
                    AppHelper.startApp(window)
                }
            } else {
                HintTool.toast(msg)
            }
        }
    }
    
}
